//package com.itheima.component;
//
////import com.itheima.service.SpringSecurityUserService;
//import com.itheima.service.SpringSecurityUserService;
//import com.itheima.service.UserService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.AuthenticationProvider;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.stereotype.Component;
//
//@Component
//public class LoginAuthenticationProvider implements AuthenticationProvider {
//
//    @Autowired
//    private SpringSecurityUserService springSecurityUserService;
//
//    @Autowired
//    private PasswordEncoder passwordEncoder;
//
//    @Override
//    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
//
//        //用户名 密码
//        String username = authentication.getName();
//        String password = (String) authentication.getCredentials();
//
//        //通过用户名获取用户信息
//        UserDetails userDetails = springSecurityUserService.loadUserByUsername(username);
//
//        if (userDetails == null) {
//            throw new UsernameNotFoundException("用户不存在！");
//        }
//        System.out.println("userDetails.getPassword() = " + userDetails.getPassword());
//
//        if (!passwordEncoder.matches(password, userDetails.getPassword())) {
//            throw new BadCredentialsException("密码不正确！");
//        }
//
//
//        return new UsernamePasswordAuthenticationToken(username, password, userDetails.getAuthorities());
//    }
//
//    /**
//     * AuthenticationManager 本身不包含认证逻辑，其核心是用来管理所有的 AuthenticationProvider，通过交由合适的 AuthenticationProvider 来实现认证。
//     * ProviderManager 是AuthenticationProvider的实现类
//     * AuthenticationManager获取所有AuthenticationProvider的实现
//     * 通过该方法判断是否支持当前方式的认证
//     * 这里支持验证UsernamePasswordAuthenticationToken
//     * @param authentication
//     * @return
//     */
//    @Override
//    public boolean supports(Class<?> authentication) {
//        return authentication.equals(UsernamePasswordAuthenticationToken.class);
//    }
//
//
//
//}
